<?php
/*
 * @分销商城
 * @功能说明：系统提现确认
 * @更新说明：暂无更新
 * @文件名 CashController.class.php
 * @编码 UTF-8
 * @创建时间 2015-12-8 下午1:51:33
 * @创建人 XING
 */
namespace dg\Controller;
use Think\Controller;
class CashController extends ApibaseController {
	/*
	 * 提现功能主体
	 * @date: 2015-12-4 上午11:56:45
	 * @editor: XING
	 */
    public function index(){
    	/*参数初始化*/
    	$page = I('get.page') ?: 1;
    	$perpage = 20;$url = array();
   		$list = M('CustCash');
    	$inCustArr = array();
    	if(I('get.cust_realname')){
    		$list = $list->where('cust_realname LIKE "%'.I('get.cust_realname').'%"');
    		$url['cust_realname'] = I('get.cust_realname');
    	}
    	if(I('get.cust_mobile')){
    		$list = $list->where(array('cust_mobile'=>I('get.cust_mobile')));
    		$url['cust_mobile'] = I('get.cust_mobile');
    	}
    	if(isset($_GET['status']) && I('get.status') > -1){
    		$list = $list->where(array('cash_status'=>I('get.status')));
    		$url['status'] = I('get.status');
    	}
    	$getObject = clone $list;
    	$getData = $list->order(array('add_at'=>'desc'))->page($page,$perpage)->select();
    	$this->assign('page',setPage($getObject->count(),$perpage,$page,'?'.http_build_query($url)));
    	$this->assign('getData',$getData);
    	
    	$this->display('cash/index');
    }
    
	/*
	 * 系统提现功能
	 * @date: 2015-12-4 下午1:57:27
	 * @editor: XING
	 */
    public function setCash(){
    	if(!in_array(I('post.status'),array(1,2)) || !I('post.cashid')){
    		jsonRespons(0, '不合法参数');
    	}
    	$vestCash = M('CustCash');
    	$vestCash->startTrans();
    	$cashAffect = $vestCash->lock(true)
    				->where(array('cust_cashid'=>I('post.cashid')))->setField(array(
    						'cash_status' => I('post.status')
    					));
    	if($cashAffect > 0){
    		 if(I('post.status') == '2'){
    		 	$vestBottler = M('CustBottler');
    			$vestBottler->startTrans();
    			$cbAffect = $vestBottler->lock(true)
    				 	->where(array('cust_cashid'=>I('post.cashid')))->setField(array(
    				 			'is_waste' => 1,
    				 	));
    			if($cbAffect > 0){
    				$vestBottler->commit();
    				$vestCash->commit();
    				jsonRespons('1','操作成功');
    			}else{
    				$vestCash->rollback();
    				$vestBottler->rollback();
    				jsonRespons('0','事务流出错，操作失败');
    			}
    		}
    		$vestCash->commit();
    		jsonRespons('1','操作成功');
    	}else{
    		$vestCash->rollback();
    		jsonRespons('0','事务流出错，操作失败');
    	}
    }
    
}